<?php


namespace app\admin\controller;


use app\admin\business\linkBus;
use app\admin\validate\LinkValidate;
use think\exception\ValidateException;
use think\facade\View;

class Link extends AdminBase
{
    public function index(){
        return View::fetch();
    }
    public function add(){
        return View::fetch();
    }
    public function edit(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function addItem(){
        $name = $this->request->param('name');
        $link_url = $this->request->param('link_url');
        $data = [
            'name' => $name,
            'link_url' => $link_url
        ];

        try {
            validate(LinkValidate::class)->scene('link_add')->check($data);
        }catch (ValidateException $exception){
            return show(config('status.error'),$exception->getError());
        }
        $linkBus = new linkBus();
        try {
            $res = $linkBus->addItemBus($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'添加成功',$res);
    }
    public function getListPage(){
        $paging = $this->request->param('paging');
        $linkBus = new linkBus();
        try {
            $res = $linkBus->getListPageBus($paging);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'添加成功',$res);
    }
    public function changeFieldByWhere(){
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        $where = [
            'id'=>$id
        ];
        $data = [
            'status'=>$status
        ];
        $linkBus = new linkBus();
        try {
            $res = $linkBus->changeFieldByWhereBus($where,$data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'更新成功',$res);
    }
    public function removeByWhere(){
        $id = $this->request->param('id');
        $where = [
            'id'=>$id
        ];
        $linkBus = new linkBus();
        try {
            $res = $linkBus->removeByWhereBus($where);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'删除成功',$res);
    }
    public function getDetailByWhere(){
        $id = $this->request->param('id');
        $where = [
            'id'=>$id
        ];
        $linkBus = new linkBus();
        try {
            $res = $linkBus->getDetailByWhereBus($where);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$res);
    }
    public function editItem(){
        $id = $this->request->param('id');
        $name = $this->request->param('name');
        $link_url = $this->request->param('link_url');
        $validateData = [
            'id' => $id,
            'name' => $name,
            'link_url' => $link_url
        ];
        try {
            validate(LinkValidate::class)->scene('link_edit')->check($validateData);
        }catch (ValidateException $exception){
            return show(config('status.error'),$exception->getError());
        }
        $where = [
            'id' => $id,
        ];
        $data = [
            'name' => $name,
            'link_url' => $link_url
        ];
        $linkBus = new linkBus();
        try {
            $res = $linkBus->editItemBus($where,$data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'更改成功',$res);
    }
}